package cn.biansc.controller;

import cn.biansc.bean.Punish;
import cn.biansc.service.IWorkflowService;
import org.activiti.engine.form.StartFormData;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value="/")
public class testController {


    @Autowired
    private IWorkflowService workflowService;


    @RequestMapping(value="/test",method = RequestMethod.GET)
    public String getIndex(){

        return "/test/index";
    }



    @ResponseBody
    @RequestMapping(value="/test/definition",method = RequestMethod.POST)
    public Map<String,Object>register(@RequestParam String processName,
                         @RequestParam MultipartFile file) throws Exception {
        Map<String,Object> m=new HashMap<String,Object>();
        if(!file.isEmpty()) {
               workflowService.saveNewDeploye(file.getInputStream(),processName);
               m.put("result","true");
               m.put("success",true);
               m.put("message","部署成功");
        }
        return m;
    }
    @RequestMapping(value = "/test/del_pd/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object>del_pd(@PathVariable String id){
        workflowService.deleteProcessDefinitionByDeploymentId(id);
        Map<String,Object>m =new HashMap<String,Object>();
        m.put("result","true");
        m.put("message","删除成功!");
        return  m;

    }
    @ResponseBody
    @RequestMapping(value="/test/list",method = RequestMethod.POST)
    public Map<String,Object> list(){

        Map<String, Object> result = new HashMap<String, Object>();

        List<Deployment> deploymentsList = workflowService.findDeploymentList();
        List<Punish> pdList=new ArrayList<>();

        for(Deployment e:deploymentsList){
            Punish punish=new Punish();
            punish.setId(e.getId());
            punish.setName(e.getName());
            punish.setDeploymentTime(e.getDeploymentTime());
            pdList.add(punish);
        }

        result.put("total", deploymentsList.size());
        result.put("rows",pdList);
        return result;
    }
    @ResponseBody
    @RequestMapping(value = "/test/tasklist",method = RequestMethod.POST)
    public Map<String,Object> taskList(@RequestParam String id,HttpSession session){
        List<Task> list = workflowService.findTaskListByName(id);


        return null;

    }


    @ResponseBody
    @RequestMapping(value = "/test/prodef",method = RequestMethod.POST)
    public Map<String,Object> pdlist(){
        Map<String,Object> result= new HashMap<String,Object>();
        List<ProcessDefinition> list = workflowService.findProcessDefinition();
        List<Punish> pdl= new ArrayList<>();

        for(ProcessDefinition pd:list){
            Punish p=new Punish();
            p.setKey(pd.getKey());
            p.setVersion(pd.getVersion());
            p.setDeploymentId(pd.getDeploymentId());
            p.setPd_id(pd.getId());
            pdl.add(p);


        }
        result.put("total",pdl.size());
        result.put("rows",pdl);
        return result;

    }

    @RequestMapping(value = "/test/startProcess/{key}", method = RequestMethod.GET)
    public String startProcess(@PathVariable String key, Model model){
        Object startFormData = workflowService.getStartFormData(key);


        model.addAttribute("startFormData",startFormData.toString());
        return "/test/start";
    }

    @ResponseBody
    @RequestMapping(value = "/test/form/",method = RequestMethod.POST)
    public Map<String,Object> getFormData(@RequestParam Map<String,Object> formMap){
        return null;
    }

}
// 搞得很烦！
//再试一次！
//在再试一次！